package com.tv.cms.goods.contorller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.tv.cms.goods.service.GoodsService;
import com.tv.cms.system.account.model.Account;
import com.tv.cms.system.admin.model.Admin;
import com.tv.cms.goods.model.Goods;
import com.tv.cms.system.admin.service.AdminService;
import com.tv.cms.system.operator.model.OperatorConfig;
import com.tv.cms.utils.InitPassword;
import com.tv.cms.utils.Result;
import com.tv.cms.utils.ResultCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * TODO
 *
 * @author
 * @date
 **/
@RestController
@RequestMapping("goods")
public class GoodsController {
    
    @Resource
    private GoodsService service;
    @Resource
    private AdminService adminService;
    
    @PostMapping("list")
    public Result<Page<Goods>> list(@RequestBody Goods goods){
        Admin admin = adminService.getById(StpUtil.getLoginIdAsInt());
        MPJLambdaWrapper<Goods> wrapper = new MPJLambdaWrapper<Goods>();
        wrapper.selectAll(Goods.class);
        wrapper.selectAs("t1.user_name", Goods:: getCreateName);
        wrapper.leftJoin(Admin.class, Admin :: getId, Goods :: getCreateBy);
        wrapper.selectAs("t2.user_name", Goods:: getUpdateName);
        wrapper.leftJoin(Admin.class, Admin :: getId, Goods :: getUpdateBy);
        wrapper.like(StringUtils.hasText(goods.getName()), Goods :: getName, goods.getName());
        wrapper.eq(goods.getStatus() != null,  Goods :: getStatus,goods.getStatus());
        if(goods.getRoleId() != null){
            wrapper.leftJoin(OperatorConfig.class, OperatorConfig :: getGoodsId, Goods :: getId);
            wrapper.eq(goods.getRoleId() != null,  OperatorConfig :: getOperatorId,admin.getOperatorId());
        }
        wrapper.orderByAsc(Goods :: getOrderId);
        Page<Goods> page = service.selectJoinListPage(new Page<>(goods.getPage(), goods.getLimit()), Goods.class, wrapper);
        Result<Page<Goods>> result = new Result<>();
        return result.OKDATA(page);
    }

    @PostMapping("save")
    public Result<Object> save(@RequestBody Goods goods){
        if(!StringUtils.hasText(goods.getName())){
            return Result.FAILURE(400,"参数错误");
        }
        Goods one = service.getOne(new QueryWrapper<Goods>().eq("name", goods.getName()));
        if(one != null && goods.getId() == null){
            return Result.FAILURE("商品已存在");
        }
        if(one != null && goods.getId() != null && !Objects.equals(goods.getId(), one.getId())){
            return Result.FAILURE("商品已存在");
        }
        boolean b;
        if(goods.getId() != null){
            goods.setUpdateBy(StpUtil.getLoginIdAsInt());
            goods.setUpdateTime(new Date());
            b = service.updateById(goods);
        }else{
            goods.setCreateBy(StpUtil.getLoginIdAsInt());
            goods.setCreateTime(new Date());
            goods.setUpdateBy(StpUtil.getLoginIdAsInt());
            goods.setUpdateTime(new Date());
            goods.setStatus(1);
            b = service.save(goods);
        }
        return b ? Result.OK() : Result.FAILURE();
    }

    /**
     * 更新角色状态
     * @param goods
     * @return
     */
    @PutMapping("status")
    public Result status(@RequestBody Goods goods){
        Goods r = new Goods();
        r.setId(goods.getId());
        r.setStatus(goods.getStatus());
        boolean b = service.updateById(r);
        return b ? Result.OK() : Result.FAILURE();
    }
    
}
